var {
  task,
  src,
  // pipe,
  dest
} = require('gulp');
// var concat = require('gulp-concat');
// var requirejs = require('gulp-requirejs-simple');
// var watch = require('gulp-watch');

var shell = require('gulp-shell');
// var sass = require('gulp-ruby-sass');
// var mainD = require('../js/config.main');
var onlineDir = 'D:/www/online/';
var webHisDir = 'E:/eye/ahis/';

/* reuqireJs 合并脚本库 */

// var bPath = '../';
// var paths = {
//   css: [bPath + 'css/*.css', '!' + bPath + 'css/*.min.css'], //需要监控变化的css文件
//   jss: [bPath + 'js/**/*.js', '!' + bPath + 'js/out/**/*.js'], //需要监控变化的js文件
//   js: bPath + 'js/'
// };

// var minJsTask = {
//   main: { //压缩bmap main.js
//     baseUrl: paths.js,
//     name: 'main.un',
//     out: paths.js + 'main.js',
//     optimize: 'uglify2',
//     uglify: {},
//     uglify2: {
//       output: {
//         screw_ie8: false,
//         beautify: true
//       },
//       compress: {
//         screw_ie8: false,
//         sequences: false,
//         global_defs: {
//           DEBUG: false
//         }
//       },
//       warnings: false,
//       mangle: false
//     },
//     mainConfigFile: paths.js + 'config.require.js',
//     watchFile: [paths.js + '*.js'] //需要监控变化的js文件
//   }
// };


// var jsTaskArr = [];

// for (taskO in minJsTask) {
//   task(taskO, requirejs(minJsTask[taskO]));
//   jsTaskArr.push(taskO);
// };

// task('minjs', jsTaskArr);



// task("minmain", function () {
//   // console.log(mainD);
//   let mainArr = [];
//   mainD.js.forEach((v, i) => {
//     console.log(v);
//     mainArr.push(bPath + 'js/' + v);
//   });
//   src(mainArr).pipe(concat('main.js')).pipe(dest(bPath + 'js/'));
// });

//task('default', requirejs(minJsTask.main));




/* 直接合并主要js文件 */
//var jp = '../js/';
//task('js', function() {
//  return src([
//	  jp+'jquery-1.11.3.min.js',
//	  jp+'zipConfig.js',
//	  jp+'plus/param.js',
//	  jp+'lib/my97/WdatePicker.js',
//	  jp+'lib/moment.min.js',
//	  jp+'lib/daterangepicker/daterangepicker.js',
//	  jp+'lib/layer/layer.js',
//	  jp+'lib/template.js',
//	  jp+'lib/easyui/jquery.easyui.min.js',
//	  jp+'lib/easyui/jquery.easyui.base.extend.js',
//	  jp+'plus/easyvalidate.extend.js',
//	  jp+'plus/jquery.extend.js',
//	  jp+'plus/pub.js',
//	  ])
//    .pipe(concat('pubmain.js'))
//    .pipe(dest(jp));
//});

var sourceDir = '../';

/* zip all 压缩所有目录js 到all ，执行 gulp minjs 方法  */
var itemDir = {
  his: {url: onlineDir + 'ahis/aier-his/aier-ui/aier-ui-ahis/src/main'},
  plat: {url: onlineDir + 'ahis/aier-his/aier-ui-platform/src/main'},
  port: {url: onlineDir + 'ahis/aier-his/aier-ui-portals/src/main'},
  amcsPort: {url: onlineDir + 'amcs/aier-ui-mcsPortals/src/main'},
  amcs: {url: onlineDir + 'amcs/aier-amcs-ui/src/main'},
  aemr: {url: onlineDir + 'aemr/aier-aemr-ui/src/main'},
  aemro: {url: onlineDir + 'aemro/aier-aemro-ui/src/main'},
  arefr: {url: onlineDir + 'arefr/aier-arefr-ui/src/main'},
  aohs: {url: onlineDir + 'aohs/aier-aohs-ui/src/main'},
  acrm: {url: onlineDir + 'acrm/aier-acrm-ui/src/main'},
  amrs: {url: onlineDir + 'amrs/aier-amrs/aier-amrs-ui/src/main'},
  alis: {url: onlineDir + 'alis/aier-alis-ui/src/main'},
  avis: {url: onlineDir + 'avis/aier-avis-ui/src/main'},
  acdr: {url: onlineDir + 'acdr/aier-acdr-ui/src/main'},
  agrv: {url: onlineDir + 'agrv/aier-agrv-ui/src/main'},
  alms: {url: onlineDir + 'alms/aier-alms-ui/src/main'},
  amms: {url: onlineDir + 'amms/aier-amms-ui/src/main'},
  awas: {url: onlineDir + 'awas/aier-awas-ui/src/main'},
  dataCenter: {url:'D:/www/aier-data-central/aier-data-central-ui/src/main'},
  dataCloud: {url: onlineDir + 'adsp/aier-data-cloud-ui/src/main'},
  web: {url: webHisDir + 'web/gulp',config:false},
  oweb: {url: webHisDir + 'oweb/gulp',config:false}
};

/* copy all 拷贝static 相关主要文件到对应项目目录覆盖  */
var toDir = {
  his: {url: onlineDir + 'ahis/aier-his/aier-ui/aier-ui-ahis/src/main/resources/static/',config:true},
  plat: {url: onlineDir + 'ahis/aier-his/aier-ui-platform/src/main/resources/static/',config:true},
  portal: {url: onlineDir + 'ahis/aier-his/aier-ui-portals/src/main/resources/static/',config:true},
  amcsPort: {url: onlineDir + 'amcs/aier-ui-mcsPortals/src/main/resources/static/',config:true},
  amcs: {url: onlineDir + 'amcs/aier-amcs-ui/src/main/resources/static/',config:true},
  aemr: {url: onlineDir + 'aemr/aier-aemr-ui/src/main/resources/static/',config:true},
  aemro: {url: onlineDir + 'aemro/aier-aemro-ui/src/main/resources/static/',config:true},
  arefr: {url: onlineDir + 'arefr/aier-arefr-ui/src/main/resources/static/',config:true},
  aohs: {url: onlineDir + 'aohs/aier-aohs-ui/src/main/resources/static/',config:true},
  amrs: {url: onlineDir + 'amrs/aier-amrs/aier-amrs-ui/src/main/resources/static/',config:true},
  alis: {url: onlineDir + 'alis/aier-alis-ui/src/main/resources/static/',config:true},
  avis: {url: onlineDir + 'avis/aier-avis-ui/src/main/resources/static/',config:true},
  acdr: {url: onlineDir + 'acdr/aier-acdr-ui/src/main/resources/static/',config:true},
  agrv: {url: onlineDir + 'agrv/aier-agrv-ui/src/main/resources/static/',config:true},
  alms: {url: onlineDir + 'alms/aier-alms-ui/src/main/resources/static/',config:true},
  amms: {url: onlineDir + 'amms/aier-amms-ui/src/main/resources/static/',config:true},
  awas: {url: onlineDir + 'awas/aier-awas-ui/src/main/resources/static/',config:true},

  dataCenter: {url:'D:/www/aier-data-central/aier-data-central-ui/src/main/resources/static/',config:false},
  dataCloud: {url: onlineDir + 'adsp/aier-data-cloud-ui/src/main/resources/static/',config:false},
  acrm: {url: onlineDir + 'acrm/aier-acrm-ui/src/main/resources/static/',config:false},
  web: {url: webHisDir + 'web/',config:false,noMainStr:true},
  oweb: {url: webHisDir + 'oweb/',config:false,noMainStr:true}
};


var copyList = [
  //	{source:'css/index.css',to:'css/'},

//  {source: 'js/lib/my97/**/*',to: 'js/lib/my97/'},
//  {source: 'js/lib/echarts/echarts.min.js',to: 'js/lib/echarts/'},
//  {source: 'js/plus/easygridEdit.extend.js',to: 'js/plus/'},
//  {source: 'js/lib/easyui/jquery.easyui.base.extend.js',to: 'js/lib/easyui/'},
//  {source: 'css/index.css',to: 'css/'},

//  {source: 'js/plus/jquery.extend.js',to: 'js/plus/'},
//  {source: 'js/plus/easygridEdit.extend.js',to: 'js/plus/'},
//  {source: 'js/plus/pub.js',to: 'js/plus/'},
//  {source: 'js/lib/layer/layer.js',to: 'js/lib/layer/'},
//  {source: 'js/lib/layer/layer.min.js',to: 'js/lib/layer/'},
//  {source: 'js/plus/cam.js',to: 'js/plus/'},
//  {source: 'js/plus/camControl.js',to: 'js/plus/'},

  {source: 'css/vars.css',to: 'css/'},
  {source: 'css/socss.css',to: 'css/'},
  {source: 'css/easy-plus.css',to: 'css/'},
  {source: 'css/plugins.css',to: 'css/'},
  {source: 'css/base.css',to: 'css/'},
  {source: 'css/soicon/**/*',to: 'css/soicon/'},
  {source: 'images/base/*',to: 'images/base/'},
  {source: 'js/plus/**/*',to: 'js/plus/'},
  {source: 'js/pvue/**/*',to: 'js/pvue/'},
  {source: 'js/lib/easyui/jquery.easyui.min.js',to: 'js/lib/easyui/'},
  {source: 'js/lib/easyui/jquery.easyui.all.js',to: 'js/lib/easyui/'},
  {source: 'js/lib/easyui/jquery.easyui.base.extend.js',to: 'js/lib/easyui/'},
  {source: 'js/lib/easyui/easyui源码修改记录.md',to: 'js/lib/easyui/'},
  {source: 'js/lib/webuploader/MyAmdWebUpload.js',to: 'js/lib/webuploader/'}

];



function copyAll() {
  for (t in toDir) {
    // console.log('copy to ========== ' + t + ' : 【' + toDir[t].url + '】');
    console.log('copy to ========== ' + t);
    copyList.forEach((v, i) => {
      return src(sourceDir + v.source).pipe(dest(toDir[t].url + v.to));
    });
  }
}

var copyConfigList = [
  {source: 'js/main.un.js',to: 'js/'},
  {source: 'js/config.require.js',to: 'js/'}
];

function copyConfig() {
  for (t in toDir) {
    if(toDir[t].config){//config 为 true 才执行
      // console.log('copy config to ========== ' + t + ' : 【' + toDir[t].url + '】');
      console.log('copy config to ========== ' + t);
      copyConfigList.forEach((v, i) => {
        return src(sourceDir + v.source).pipe(dest(toDir[t].url + v.to));
      });
    }
  }
}


var gulpfileList = [
  {source: 'main/gulp-mini.bat',to: ''},
  {source: 'main/gulpfile.js',to: ''},
  {source: 'main/node_modules.zip',to: ''},
  {source: 'main/package.json',to: ''}
];

function copyGulpfile() {
  for (t in itemDir) {
    if(itemDir[t].config!==false){//config 为 true 才执行
      // console.log('copy config to ========== ' + t + ' : 【' + toDir[t].url + '】');
      console.log('copyGulpfile to ========== ' + t);
      gulpfileList.forEach((v, i) => {
        return src(sourceDir + v.source).pipe(dest(itemDir[t].url + v.to));
      });
    }
  }
}

function copy(tt) {
  // console.log('copy to ========== ' + tt + ' : 【' + toDir[tt].url + '】');
  console.log('copy to ========== ' + tt);
  copyList.forEach((v, i) => {
    return src(sourceDir + v.source)
      .pipe(dest(toDir[tt].url + v.to));
  });
}


task('copyall', function (cb) { //拷贝到所有目录
  copyAll();
  cb();
});
task('call', function (cb) { //拷贝到所有目录 短命名
  copyAll();
  cb();
});
task('ca', function (cb) { //拷贝到所有目录 短命名
  copyAll();
  cb();
});

task('copyconfig', function (cb) { //拷贝config到所有目录
  copyConfig();
  cb();
});
task('cc', function (cb) { //拷贝config到所有目录 短命名
  copyConfig();
  cb();
});

task('copygulpfile', function (cb) { //拷贝config到所有目录
  copyGulpfile();
  cb();
});
task('cg', function (cb) { //拷贝config到所有目录 短命名
  copyGulpfile();
  cb();
});


for (tt in toDir) {
  (function (tt) {
    task('copy' + tt, function (cb) { //根据项目名执行拷贝
      copy(tt);
      cb();
    });

    task('c' + tt, function (cb) { //根据项目名执行拷贝 短命名
      copy(tt);
      cb();
    });

  })(tt);
}





async function zipSync(item) {
  return await new Promise(resolve => {
    let path = itemDir[item].url;
    console.log('==========', item);
    src(path, {
      read: false
    }).pipe(shell('gulp', {
      cwd: path
    })).pipe(() => {
      resolve(item);
    });
  });
}

function zipAll() {
  for (p in itemDir) {
    let path = itemDir[p].url;
    console.log('====== zip task:', p, ' ======');
    src(path, {
      read: false
    }).pipe(shell('gulp', {
      cwd: path
    }));
  }
}
function markMainAll(type) {
  let timeStr = new Date().getTime();
  let taskOrder = 'gulp markMain'+(type=='m'?'':' --str '+timeStr);
  Object.keys(itemDir).forEach(function(k){
	let item = itemDir[k]
    let path = item.url;
	if(item.noMainStr){return false;}
    console.log('====== markMainTimeStr task:', k, taskOrder, ' ======');
	  src(path, {
		read: false
	  }).pipe(shell(taskOrder, {
		cwd: path
	  }));
  });
}
function markCssAll(type) {
  let timeStr = new Date().getTime();
  let taskOrder = 'gulp markCss'+(type=='m'?'':' --str '+timeStr);
  Object.keys(itemDir).forEach(function(k){
	let item = itemDir[k]
    let path = item.url;
	if(item.noMainStr){return false;}
    console.log('====== markCssTimeStr task:', k, taskOrder, ' ======');
	  src(path, {
		read: false
	  }).pipe(shell(taskOrder, {
		cwd: path
	  }));
  });
}

function zipItem(p) {
  console.log('====== zip task:', p, ' ======');
  let path = itemDir[p].url;
  src(path, {
    read: false
  }).pipe(shell('gulp', {
    cwd: path
  }));
}

function markMainItem(p,type) {
  console.log('====== markMainTimeStr task:', p, ' ======');
  let path = itemDir[p].url;
  let timeStr = new Date().getTime();
  let taskOrder = 'gulp markMain'+(type=='m'?'':' --str '+timeStr);
  src(path, {
    read: false
  }).pipe(shell(taskOrder, {
    cwd: path
  }));
}

function markCssItem(p,type) {
  console.log('====== markCssTimeStr task:', p, ' ======');
  let path = itemDir[p].url;
  let timeStr = new Date().getTime();
  let taskOrder = 'gulp markCss'+(type=='m'?'':' --str '+timeStr);
  src(path, {
    read: false
  }).pipe(shell(taskOrder, {
    cwd: path
  }));
}

task('zipall', (cb) => {
  zipAll();
  cb();
});
task('zall', (cb) => {
  zipAll();
  cb();
});
task('za', (cb) => {
  zipAll();
  cb();
});

task('markMainTAll', (cb) => {//所有 main.js 打时间戳版本号
  markMainAll('t');
  cb();
});
task('mtall', (cb) => {
  markMainAll('t');
  cb();
});
task('mta', (cb) => {
  markMainAll('t');
  cb();
});

task('markCssTAll', (cb) => {//所有 base css 打时间戳版本号
  markCssAll('t');
  cb();
});
task('mca', (cb) => {
  markCssAll('t');
  cb();
});

task('markMainMAll', (cb) => {//所有 main.js 打md5版本号
  markMainAll('m');
  cb();
});
task('mmall', (cb) => {
  markMainAll('m');
  cb();
});
task('mma', (cb) => {
  markMainAll('m');
  cb();
});



for (it in itemDir) {
  (function (it) {
    task('zip' + it, function (cb) { //根据项目名执行压缩
      zipItem(it);
      cb();
    });

    task('z' + it, function (cb) { //根据项目名执行压缩 短命名
      zipItem(it);
      cb();
    });

    task('mt' + it, function (cb) { //根据项目名执行压缩 短命名
      markMainItem(it,'t');
      cb();
    });
    task('mc' + it, function (cb) { //根据项目名执行压缩 短命名
      markCssItem(it,'t');
      cb();
    });

	task('mm' + it, function (cb) { //根据项目名执行压缩 短命名
      markMainItem(it,'m');
      cb();
    });

  })(it);
}
